using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using XinBang.Entity;
using System.Collections.Generic;
using XinBang.BLL;
using XinBang.Util;
using XinBang.Session;

namespace XinBang.Web
{
    public partial class ShoppingOrderPage : System.Web.UI.Page
    {
        private OrderBL ob = new OrderBL();
        private SendBL sb = new SendBL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    IList<Order> list = ob.GetAllOrdersByMember(SessionUtil.GetMemberSession());
                    Order lastOrder = list[0];
                    BindLastOrder(lastOrder);
                    list.RemoveAt(0);
                    BindOrderListInPast(list);
                    BindSendMethod(lastOrder);

                }
                catch (MissSessionException)
                {
                    Response.Redirect("~/Admin/Index.aspx");
                }
            }
        }

        private void BindSendMethod(Order order)
        {
            sendMethod.DataSource = sb.GetAllSends();
            sendMethod.DataTextField = "Describe";
            sendMethod.DataValueField = "Senid";
            sendMethod.DataBind();
            Send send = sb.GetSendById(Convert.ToInt32(sendMethod.SelectedValue));
            sendCost.Text = string.Format("{0:c}", send.Cost);
            amountCost.Text = string.Format("{0:c}",(order.OrderValue + send.Cost));
        }

        private void BindLastOrder(Order lastOrder)
        {
            IList<Order> list = new List<Order>();
            list.Add(lastOrder);
            currentOrder.DataSource = list;
            currentOrder.DataBind();
            goodsPrice.Value = lastOrder.OrderValue.ToString();
            if (lastOrder.SendTime != null)
            {
                confirm.Enabled = false;
            }
            if (lastOrder.PayTime != null)
            {
                topay.Visible = false;
            }
        }

        private void BindOrderListInPast(IList<Order> list)
        {
            shoppingOrderList.DataSource = list;
            shoppingOrderList.DataBind();
        }

        protected void sendMethod_SelectedIndexChanged(object sender, EventArgs e)
        {
            Send send = sb.GetSendById(Convert.ToInt32(sendMethod.SelectedValue));
            sendCost.Text = string.Format("{0:c}", send.Cost);
            amountCost.Text = string.Format("{0:c}", (Convert.ToDecimal(goodsPrice.Value) + send.Cost));
        }

        protected void confirm_Click(object sender, EventArgs e)
        {
            WebApplication.GetCurrentSession().BeginTransaction();
            Order order = ob.GetLastOrderByMember(SessionUtil.GetMemberSession());
            order.OrderTime = DateTime.Now;
            order.Senid = sb.GetSendById(Convert.ToInt32(sendMethod.SelectedValue));
            ob.UpdateOrder(order);
            WebApplication.GetCurrentSession().Transaction.Commit();
            BindLastOrder(order);
        }
    }
}
